package com.smart.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smart.common.PageInfo;
import com.smart.common.utils.AssertUtil;
import com.smart.context.SessionContext;
import com.smart.mapper.ProjectFileTaskMapper;
import com.smart.model.domain.ProjectFileTaskDO;
import com.smart.model.dtomapper.ProjectFileTaskInstanceDtoMapper;
import com.smart.model.domain.ProjectFileTaskInstanceDO;
import com.smart.mapper.ProjectFileTaskInstanceMapper;
import com.smart.model.request.ProjectFileTaskInstanceRequest;
import com.smart.service.ProjectFileTaskInstanceService;
import com.smart.model.convert.ProjectFileTaskInstanceConvert;
import com.smart.model.vo.ProjectFileTaskInstanceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class ProjectFileTaskInstanceServiceImpl extends ServiceImpl<ProjectFileTaskInstanceMapper, ProjectFileTaskInstanceDO> implements ProjectFileTaskInstanceService {
    @Autowired
    private ProjectFileTaskInstanceMapper projectFileTaskInstanceMapper;
    @Autowired
    private ProjectFileTaskMapper projectFileTaskMapper;

    @Override
    public ProjectFileTaskInstanceVO getByKey(long id) {
        ProjectFileTaskInstanceDO projectFileTaskInstanceDO = getById(id);
        ProjectFileTaskInstanceVO projectFileTaskInstanceVO = ProjectFileTaskInstanceDtoMapper.INSTANCE.do2VO(projectFileTaskInstanceDO);
        return projectFileTaskInstanceVO;
    }

    @Override
    public PageInfo<ProjectFileTaskInstanceVO> pageQuery(ProjectFileTaskInstanceRequest request) {
        AssertUtil.assertNotNull(SessionContext.getProjectId(), "项目id不能为空");
        request.setProjectId(SessionContext.getProjectId());
        long count = projectFileTaskInstanceMapper.pageCount(request);
        List<ProjectFileTaskInstanceDO> list = projectFileTaskInstanceMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<ProjectFileTaskInstanceVO> projectFileTaskInstanceVOList = ProjectFileTaskInstanceConvert.buildProjectFileTaskInstanceVoList(list);
        fillInfo(projectFileTaskInstanceVOList);
        return new PageInfo(request.getPageNum(), request.getPageSize(), projectFileTaskInstanceVOList, count);
    }

    private void fillInfo(List<ProjectFileTaskInstanceVO> instances) {
        for (ProjectFileTaskInstanceVO instance : instances) {
            ProjectFileTaskDO task = projectFileTaskMapper.selectDOById(instance.getTaskId());
            if (task != null) {
                instance.setFileCode(task.getFileCode());
                instance.setFileName(task.getFileName());
                instance.setFileUrl(task.getFileUrl());
            }
        }
    }

    @Override
    public List<ProjectFileTaskInstanceVO> listQuery(ProjectFileTaskInstanceRequest request) {
        List<ProjectFileTaskInstanceDO> list = list();
        List<ProjectFileTaskInstanceVO> projectFileTaskInstanceVOS = ProjectFileTaskInstanceConvert.buildProjectFileTaskInstanceVoList(list);
        return projectFileTaskInstanceVOS;
    }

    @Override
    public Boolean add(ProjectFileTaskInstanceRequest request) {
        ProjectFileTaskInstanceDO projectFileTaskInstanceDO = ProjectFileTaskInstanceDtoMapper.INSTANCE.request2DO(request);
        save(projectFileTaskInstanceDO);
        return true;
    }

    @Override
    public Boolean updateByKey(ProjectFileTaskInstanceRequest request) {
        ProjectFileTaskInstanceDO projectFileTaskInstanceDO = ProjectFileTaskInstanceDtoMapper.INSTANCE.request2DO(request);
        save(projectFileTaskInstanceDO);
        return true;
    }

    @Override
    public Boolean deleteByKey(long id) {
        projectFileTaskInstanceMapper.deleteDOById(id);
        return true;
    }

    @Override
    public Boolean receive(long id) {
        ProjectFileTaskInstanceDO instanceDO = projectFileTaskInstanceMapper.selectDOById(id);
        AssertUtil.assertNotNull(instanceDO, "数据不存在");
        instanceDO.setStatus(1);
        instanceDO.setReceiveTime(new Date());
        saveOrUpdate(instanceDO);
        return true;
    }
}
